<?php

namespace App\Http\Controllers\Admin\AirLine;

use App\Http\Controllers\Controller;
use App\Models\AttachmentModel;
use App\Models\DestinationRuleModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

//附件管理
class Attachment extends Controller
{

    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $name = $request->input('name', '');
        $type = $request->input('type', '');
        $create_start = $request->input('startTime', '');
        $create_end = $request->input('endTime', '');

        $query = AttachmentModel::query()
            ->with('create_admin');

        if($name){
            $query->where('name', 'like', '%'.$name.'%');
        }

        if($type){
            $query->where('type', $type);
        }

        if($create_start && $create_end){
            $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            return [
                'id' => $item->id,
                'name' => $item->name,
                'type' => $item->type,
                'file' => $item->file,
                'type_name' => AttachmentModel::TYPE_MAPS[$item->type],
                'created_at' => $item->created_at->toDateTimeString(),
                'create_user' => $item->create_admin->staff->name ?? '',
            ];
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $validator = Validator::make($params, [
            'name' => 'required',
            'type' => 'required',
            'file' => 'required',
        ], [
            'name.required' => '请输入附件名称',
            'type.required' => '请选择附件类型',
            'file.required' => '请上传附件',
        ]);

        if($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $e = AttachmentModel::where('name', $params['name'])->first();
        if($e) {
            return $this->error('附件名称已存在');
        }

        $attachment = new AttachmentModel();
        $attachment->name = $params['name'];
        $attachment->type = $params['type'];
        $attachment->file = $params['file'];
        $attachment->created_admin_id = $auth->id;
        $attachment->save();
        return $this->success();
    }

    public function update(Request $request, $id){
        $params = $request->all();

        $validator = Validator::make($params, [
            'name' => 'required',
            'type' => 'required',
            'file' => 'required',
        ], [
            'name.required' => '请输入附件名称',
            'type.required' => '请选择附件类型',
            'file.required' => '请上传附件',
        ]);

        if($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $e = AttachmentModel::where('name', $params['name'])
            ->where('id', '<>', $id)->first();
        if($e) {
            return $this->error('附件名称已存在');
        }

        $attachment = AttachmentModel::find($id);
        if(!$attachment) {
            return $this->error('附件不存在');
        }

        $attachment->name = $params['name'];
        $attachment->type = $params['type'];
        $attachment->file = $params['file'];
        $attachment->save();
        return $this->success();
    }

    public function delete(int $id){
        AttachmentModel::destroy($id);
        return $this->success();
    }

}